
#include"ringQueue.hpp"

#define NUM 5

using namespace std;




void* producer(void* args)
{
    RingQueue<int> *rq = (RingQueue<int>*) args;

    while(true)
    {
        int x = rand() % 100 + 17;

        std::cout << "生产: " << x << " [" << pthread_self() << "]" << std::endl;
        rq->push(x);
    }
}

void* consumer(void* args)
{
    RingQueue<int> *rq = (RingQueue<int>*) args;

    while(true)
    {
        int x = 0;
        rq->pop(&x);
        std::cout << "消费: " << x << " [" << pthread_self() << "]" << std::endl;
    }
}





int main()
{

    srand((uint64_t)time(nullptr) ^ getpid());
    pthread_t c[NUM], p[NUM];
    RingQueue<int>* pq = new RingQueue<int>;
    pthread_create(p, nullptr, producer, (void*)pq);
    pthread_create(p + 1, nullptr, producer, (void*)pq);
    pthread_create(p + 2, nullptr, producer, (void*)pq);
    pthread_create(p + 3, nullptr, producer, (void*)pq);
    pthread_create(p + 4, nullptr, producer, (void*)pq);

    pthread_create(c, nullptr, consumer, (void*)pq);
    pthread_create(c + 1, nullptr, consumer, (void*)pq);
    pthread_create(c + 2, nullptr, consumer, (void*)pq);
    pthread_create(c + 3, nullptr, consumer, (void*)pq);
    pthread_create(c + 4, nullptr, consumer, (void*)pq);

    for(int i = 0; i < 5; i++) pthread_join(c[i], nullptr);
    for(int i = 0; i < 5; i++) pthread_join(p[i], nullptr);
    return 0;


}